package de.baumann.browser.api.net.base;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiBuilder {
    private static Gson gson;
    private static OkHttpClient.Builder httpClient;
    private static HttpLoggingInterceptor logging;

    static {
        gson = new GsonBuilder()
                .registerTypeAdapter(int.class, new IntegerTypeAdapter())
                .registerTypeAdapter(Integer.class, new IntegerTypeAdapter())
                .registerTypeAdapter(double.class, new DoubleTypeAdapter())
                .registerTypeAdapter(Double.class, new DoubleTypeAdapter())
                .registerTypeAdapter(String.class,new StringTypeAdapter())
                .create();

        logging = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);

        httpClient = new OkHttpClient.Builder()
                .connectTimeout(1, TimeUnit.MINUTES)
                .writeTimeout(1, TimeUnit.MINUTES)
                .readTimeout(1, TimeUnit.MINUTES)
                .sslSocketFactory(RxUtils.createSSLSocketFactory())
                .addInterceptor(new RequestInterceptor())
//                .addInterceptor(new CommonInterceptor())
                .addInterceptor(logging);
    }


    public static <S> S buildService(Class<S> serviceClass) {
        BaseUrl urlAnnotation = serviceClass.getAnnotation(BaseUrl.class);
        String baseUrl = urlAnnotation.value();
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(httpClient.build())
                .build()
                .create(serviceClass);
    }

    static Interceptor headInterceptor = chain -> {
        Request build = chain.request().newBuilder()
                .removeHeader("User-Agent")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4)")
                .addHeader("Content-Type", "application/json")
                .build();
        return chain.proceed(build);
    };

    public static <T> T buildNewsService(Class<T> serviceClass) {
        BaseUrl urlAnnotation = serviceClass.getAnnotation(BaseUrl.class);
        String baseUrl = urlAnnotation.value();
        httpClient.addInterceptor(headInterceptor);
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(httpClient.build())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build().create(serviceClass);
    }
}
